package com.example.mobile2030606148tdh0601;

import androidx.appcompat.app.AppCompatActivity;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {
    //记住记住账号密码:根据复选框的选择状态，保存和读取账号密码
    //步骤：1、定义sp对象
    //2.给复选框设置状态改变的监听器
    //3.在监听器处理单元中，保存选择状态。
    //4.读取选择状态，并赋值给控件。（复选框控件）
    //5.给登录按钮添加监听事件，读取输入的账号和密码
    //6.当账号和密码正确的时候，根据复选框选择状态，决定要不要保存账号
    //7.根据复选框的选择状态，读取保存在sp中的用户名

    private EditText edtTxtMainUserName;
    private EditText edtTxtMainPassword;
    private CheckBox chkBoxMainPSW;
    private CheckBox chkBoxMainName;
    private Button btnMainLogin;
    private Button btnMainReset;

    private SharedPreferences sp;//声明对象

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        sp=getSharedPreferences("login",MODE_PRIVATE);
        chkBoxMainName.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                SharedPreferences.Editor editor=sp.edit();//sp编辑器
                editor.putBoolean("chkName",isChecked);//选择状态布尔数据存入到sp中
                editor.commit();//提交
            }
        });
        chkBoxMainPSW.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                SharedPreferences.Editor editor=sp.edit();
                editor.putBoolean("chkPsw",isChecked);
                editor.commit();
                if (isChecked){
                    chkBoxMainName.setChecked(true);
                    chkBoxMainPSW.setEnabled(false);
                }else{
                    chkBoxMainName.setEnabled(true);
                }
            }
        });
        btnMainLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //当账号密码都正确时，根据chkBoxMainName状态保存数据
                String userName=edtTxtMainUserName.getText().toString();//.trim()
                String password=edtTxtMainPassword.getText().toString();//.trim();
                if (userName.equals("admin")&&password.equals("admin")){
                    if (chkBoxMainName.isChecked()){
                        SharedPreferences.Editor editor=sp.edit();//获得sp编译器
                        editor.putString("userName",userName);//存入字符串类型数据/选择用户名数据存入到sp中
                        editor.commit();//提交数据修改
                    }
                    if (chkBoxMainPSW.isChecked()){
                        SharedPreferences.Editor editor=sp.edit();
                        editor.putString("password",password);
                        editor.commit();
                    }
                }
            }
        });
    }

    @Override
    protected void onStart() {
        super.onStart();
        boolean chkName=sp.getBoolean("chkName",false);
        chkBoxMainName.setChecked(chkName);//保存复选框的选择状态
        boolean chkPSW=sp.getBoolean("chkPSW",false);
        chkBoxMainPSW.setChecked(chkPSW);

        if (chkName){
            edtTxtMainUserName.setText(sp.getString("userName",null));

        }
        if (chkPSW){
            edtTxtMainPassword.setText(sp.getString("password",null));
        }
    }

    private void initView() {
        edtTxtMainUserName = (EditText) findViewById(R.id.edtTxtMainUserName);
        edtTxtMainPassword = (EditText) findViewById(R.id.edtTxtMainPassword);
        chkBoxMainPSW = (CheckBox) findViewById(R.id.chkBoxMainPSW);
        chkBoxMainName = (CheckBox) findViewById(R.id.chkBoxMainName);
        btnMainLogin = (Button) findViewById(R.id.btnMainLogin);
        btnMainReset = (Button) findViewById(R.id.btnMainReset);
    }
}